/**
 * @fileOverview Utils functions
 * 
 * @author Adam Hořčica horcicaa@gmail.com
 * @url $HeadURL: http://ahs-gadgets.googlecode.com/svn/trunk/bb-counters/src/utils.js $
 * @revision $Id: utils.js 47 2008-02-09 20:30:13Z horcicaa $
 */

/**
 * Shortcut for document.getElementById
 */ 
function $(id)
{
  return document.getElementById(id);
}

/**
 * Shortcut for appendChild
 */ 
$.a = function(to, what)
{
  to.appendChild(what);
  return to;
}

/**
 * Shortcut for document.createTextNode
 */ 
$.txt = function(text)
{
  return document.createTextNode(text);
}

/**
 * Shortcut for createElement
 */ 
$.c = function(name, className, content)
{ 
  var el = document.createElement(name);
  
  if(className)
    el.className = className;
  
  if(content)
    if(typeof content == "string")
      el.innerHTML = content;
    else
      $.a(el, content);
      
  return el;
}

/**
 * Template function
 */ 
function $TPL(template, values)
{
  var reg = /{(\w+)}/g; /* ... {name} ... */
  
  function tpl(values)
  {
    if(typeof values == "object")
      return template.replace(reg, function(pattern, name){
        return (values[name] !== undefined) ? values[name] : pattern;
      });
    else
      return template;
  }
  
  return (values !== undefined) ? tpl(values) : tpl;
}
